const Http = {

    createXmlHttp: function () {
        let xmlHttp = null;
        if (window.ActiveXObject) {
            try {
                xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }

        } else if (window.XMLHttpRequest) {
            try {
                xmlHttp = new XMLHttpRequest();
            } catch (e) {
                xmlHttp = false;
            }
        } else {
            console.log("您的浏览器不支持xmlHttpRequest对象");
        }
        return xmlHttp;
    },

    get: function (url, success, error) {
        let xmlHttp = this.createXmlHttp();
        xmlHttp.onreadystatechange = function () {
            switch (xmlHttp.readyState) {
                case 1:
                    console.log("after open(), before send() : request not send. ");
                    break;
                case 2:
                    console.log("after send(), service revived request but not response.");
                    break;
                case 3:
                    console.log("get all response header,but not recive response body.");
                    break;
                case 4:
                    if (xmlHttp.status == 200) {
                        success(xmlHttp.responseText);
                    } else {
                        console.log("get all response body, but status is not 200.");
                    }
                    break;
                default:
                    console.log("error occured.");
                    error(xmlHttp.statusText);
                    break;
            }
        }
        xmlHttp.open('get', url, true);
        xmlHttp.setRequestHeader("Accept", "application/json");
        xmlHttp.setRequestHeader("Content-Type", "application/json");
        xmlHttp.send();
    },

    post: function (url, data, success, fail) {
        let xmlHttp = this.createXmlHttp();
        xmlHttp.onreadystatechange = function () {
            switch (xmlHttp.readyState) {
                case 1:
                    console.log("after open(), before send() : request not send. ");
                    break;
                case 2:
                    console.log("after send(), service revived request but not response.");
                    break;
                case 3:
                    console.log("get all response header,but not recive response body.");
                    break;
                case 4:
                    if (xmlHttp.status == 200) {
                        success(xmlHttp.responseText);
                    } else {
                        console.log("get all response body, but status is not 200.");
                    }
                    break;
                default:
                    console.log("error occured.");
                    error(xmlHttp.statusText);
                    break;
            }
        }
        xmlHttp.open('post', url, true);
        xmlHttp.setRequestHeader("Accept", "application/json");
        xmlHttp.setRequestHeader("Content-Type", "application/json");
        xmlHttp.send(JSON.stringify(data));
    }
};

export default Http;